home *** CD-ROM | disk | FTP | other *** search
/ Megaware 1 / Megaware Volume 1.iso / programg / c-tutor / chap11.txt < prev    next >
Text File  |  1990-07-20  |  15KB  |  354 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 11
  5.                                            MORE VIRTUAL FUNCTIONS
  6.  
  7. This chapter will actually be a continuation of the topics covered
  8. in the last chapter but this will be a fuller explanation of what
  9. virtual functions are and how they can be used in a program.  We
  10. will present a simple database program with a virtual function to
  11. show how it can be used, then we will go on to illustrate a more
  12. complex use of the virtual function in a manner that finally
  13. illustrates its utility and reason for existence.
  14.  
  15.  
  16. HOW TO START AN OOP PROJECT
  17. _________________________________________________________________
  18.  
  19. The observant student will notice that we begin our use of object
  20. oriented programming by identifying an object, or in this case a
  21. class of objects and even some subordinate objects, which we
  22. completely define.  When we get to the main program we then have
  23. a simple job with the remaining needs and they are completed using
  24. standard procedural programming techniques which we are familiar
  25. with.  This is the way to begin any object oriented programming
  26. project, by first identifying a few objects that can be separated
  27. conveniently from the rest of the code, programming them, then
  28. writing the main program.  It should be added that, for your first
  29. project using objects, do not try to make everything an object. 
  30. Select a few objects and after gaining experience with object
  31. oriented programming techniques, use more objects on future
  32. projects.  Most programmers use too many objects for their first
  33. project and write very obtuse, unreadable code.
  34.  
  35.  
  36. THE PERSON HEADER FILE
  37. _________________________________________________________________
  38.  
  39. Examine the file named PERSON.HPP for the        ================
  40. definition file for the person class.  This         PERSON.HPP
  41. class definition should cause you no problem to  ================
  42. understand since there is nothing new here.  The
  43. only thing that should be mentioned about this
  44. class is that the protected mode is used for the variables so that
  45. they are readily available in the subclasses which will import this
  46. class.
  47.  
  48.  
  49. THE PERSON IMPLEMENTATION
  50. _________________________________________________________________
  51.  
  52. The implementation for the person class is given ================
  53. here and it is a little strange in the way it is    PERSON.CPP
  54. written and used.  The intent of this program is ================
  55. that the virtual method named display() in this
  56. file will never be used, but it is required by
  57.  
  58.                                                         Page 11-1
  59.  
  60.                               Chapter 11 - More Virtual Functions
  61.  
  62. the C++ compiler to be used for a default in case some of the
  63. subclasses do not have this function available.  In the main
  64. program we will be careful to never call this function due to the
  65. nature of the program we are writing.  Keep in mind that C++
  66. requires an implementation of all virtual functions even if they
  67. are never used.  In this case the message is obviously intended to
  68. be output as an error message.
  69.  
  70. Be sure to compile this program prior to going on to the next class
  71. definitions.
  72.  
  73.  
  74. THE SUPERVISOR HEADER
  75. _________________________________________________________________
  76.  
  77. The file named SUPERVSR.HPP contains the class   ================
  78. definitions for the three derived classes,         SUPERVSR.HPP
  79. supervisor, programmer, and secretary.  These    ================
  80. were all placed in a single file for two
  81. reasons.  The first reason is to simply
  82. illustrate to you that this can be done, and secondly, to allow
  83. some of the files to be combined on the disk and to require fewer
  84. compilations by you prior to executing the resulting program.  This
  85. is actually a good way to combine these files since they are all
  86. subclasses of a common class.  It is actually a matter of style or
  87. personal taste.
  88.  
  89. You will notice that all three of these classes contain a method
  90. named display() and all have the same return value of void, and all
  91. have the same number of parameters as the parent class's method of
  92. the same name.  All of this equality is required because they are
  93. all actually called by the same call statement.  You will also
  94. notice that the other method in each class has the same name, but
  95. different numbers and types of formal parameters which prevents
  96. this method from being used as a virtual method.
  97.  
  98. The remainder of this file is simple and you should be able to read
  99. the code and understand it completely.  Once again, this file
  100. cannot be compiled or executed.
  101.  
  102.  
  103. THE SUPERVISOR IMPLEMENTATION
  104. _________________________________________________________________
  105.  
  106. The file named SUPERVSR.CPP contains the         ================
  107. implementation for the three classes.  If you      SUPERVSR.CPP
  108. spend a little time studying the code, you will  ================
  109. find that each of the methods named init_data()
  110. simply initializes all fields to those passed in
  111. as the actual arguments in a very simple manner.
  112.  
  113. The method named display(), however, outputs the stored data in
  114. different ways for each class since the data is so different in
  115. each of the classes.  Even though the interface to these three
  116.  
  117.                                                         Page 11-2
  118.  
  119.                               Chapter 11 - More Virtual Functions
  120.  
  121. methods is identical, the actual code is significantly different. 
  122. There is no reason code besides output could not have been used,
  123. but the output is so visible when the program is executed that it
  124. was chosen for this illustration.
  125.  
  126. This file should be compiled at this time in preparation for the
  127. next example program which will use all four classes as defined in
  128. these four files.
  129.  
  130.  
  131. THE FIRST CALLING PROGRAM
  132. _________________________________________________________________
  133.  
  134. The file named EMPLOYEE.CPP is the first program ================
  135. that uses the classes developed in this chapter,   EMPLOYEE.CPP
  136. and you will find that it is a very simple       ================
  137. program.
  138.  
  139. We begin with an array of ten pointers, each pointing to the parent
  140. class.  As you recall from the last chapter, this is very important
  141. when using virtual functions, the pointer must point to the parent
  142. class.  The pointers that will be stored in this array will all
  143. point to objects of the subclasses however.  When we use the
  144. resulting pointers to refer to the methods, the system will choose
  145. the method at run time, not at compile time as nearly all of our
  146. other programs have been doing.
  147.  
  148. We allocate six objects in lines 16 through 39, initialize them to
  149. some values using the methods named init_data(), then assign the
  150. pointers to the members of the array of pointers to person. 
  151. Finally, in lines 41 and 42, we call the methods named display()
  152. to display the stored data on the monitor.  You will notice that
  153. even though we only use one method call in line 42, we actually
  154. send messages to each of the three methods named display() in the
  155. subclasses.  This is true dynamic binding because if we were to
  156. change the values of some of the pointers in the array, we would
  157. then call different methods with the same pointers.
  158.  
  159. Be sure to compile and execute this program before continuing on
  160. in this chapter.  You will recall that the linking step requires
  161. you to combine several files in order to satisfy all system calls. 
  162. After you have done that, we will use the same objects in another
  163. way to show how they can be reused.
  164.  
  165.  
  166. THE LINKED LIST CLASS
  167. _________________________________________________________________
  168.  
  169. Examination of the file named ELEMLIST.HPP will  ================
  170. reveal the definition of two more classes which    ELEMLIST.HPP
  171. will be used to build a linked list of employees ================
  172. to illustrate a more practical way to use the
  173. dynamic binding we have been studying in this
  174. chapter.
  175.  
  176.                                                         Page 11-3
  177.  
  178.                               Chapter 11 - More Virtual Functions
  179.  
  180.  
  181. The two classes were put in the same file because they work
  182. together so closely and neither is of much value without the other. 
  183. You will notice that the elements of the linked list do not contain
  184. any data, only a pointer to the person class that we developed for
  185. the last program, so that the linked list will be composed of
  186. elements of the person class without modifying the class itself.
  187.  
  188. There are two interesting constructs used here that must be pointed
  189. out before going on to the next program.  The first is the partial
  190. declaration given in line 8 which allows us to refer to the class
  191. named employee_list before we actually define it.  The complete
  192. declaration for the class is given in lines 22 through 29.  The
  193. second construct of interest is the friend class listed in line 17
  194. where we give the entire class named employee_list free access to
  195. the variables which are a part of the employee_element class.  This
  196. is necessary because the method named add_person() must access the
  197. pointers contained in employee_element.  We could have defined an
  198. additional method as a part of employee_element and used this
  199. method to refer to the pointers but it was felt that these two
  200. classes work so well together that it is not a problem to open a
  201. window between the classes.  We still have complete privacy from
  202. all other programs and classes declared as parts of this program.
  203.  
  204. Note that the single method included in the employee_element class
  205. is implemented in inline code.  Two of the methods of employee_list
  206. are still open so we need an implementation for this class.
  207.  
  208.  
  209. THE LINKED LIST IMPLEMENTATION
  210. _________________________________________________________________
  211.  
  212. The file named ELEMLIST.CPP is the               ================
  213. implementation for the linked list classes and     ELEMLIST.CPP
  214. should be self explanatory if you understand how ================
  215. a singly linked list operates.  All new elements
  216. are added to the end of the current list.  This
  217. was done to keep it simple but a sorting mechanism could be added
  218. to sort the employees by name if desired.
  219.  
  220. The method to display the list simply traverses the list and calls
  221. the method named display() in line 30 once for each element of the
  222. list.
  223.  
  224. It is important for you to take notice that in this entire class,
  225. there is no mention made of the existence of the three derived
  226. classes, only the parent class named person is mentioned.  The
  227. linked list therefore has no hint that the three subclasses even
  228. exist, but in spite of that, we will see this class send messages
  229. to the three subclasses as they are passed through this logic. 
  230. That is exactly what dynamic binding is, and we will have a little
  231. more to say about it after we examine the calling program.
  232.  
  233.  
  234.  
  235.                                                         Page 11-4
  236.  
  237.                               Chapter 11 - More Virtual Functions
  238.  
  239. THE LINKED LIST IMPLEMENTATION
  240. _________________________________________________________________
  241.  
  242. At this time you should examine the final        ================
  243. example program in this chapter named              EMPLOYE2.CPP
  244. EMPLOYE2.CPP for our best example of dynamic     ================
  245. binding in this tutorial, yet the program is
  246. kept very simple.
  247.  
  248. This program is very similar to the example program named
  249. EMPLOYEE.CPP with a few changes to better illustrate dynamic
  250. binding.  In line 7 we declare an object of the class employee_list
  251. to begin our linked list.  This is the only copy of the list we
  252. will need for this program.  For each of the elements, we allocate
  253. the data, fill it, and send it to the linked list to be added to
  254. the list where we allocate another linked list element to point to
  255. the new data, and add it to the list.  The code is very similar to
  256. the last program down through line 40.
  257.  
  258. In line 43 we send a message to the display_list() method which
  259. outputs the entire list of personnel.  You will notice that the
  260. linked list class defined in the files named ELEMLIST.HPP and
  261. ELEMLIST.CPP are never informed in any way that the subclasses even
  262. exist but they dutifully pass the pointers to these subclasses to
  263. the correct methods and the program runs as expected.
  264.  
  265.  
  266. WHAT GOOD IS ALL OF THIS
  267. _________________________________________________________________
  268.  
  269. Now that we have the program completely debugged and working,
  270. suppose that we wished to add another class to the program, for
  271. example a class named consultant because we wished to include some
  272. consultants in our business.  We would have to write the class of
  273. course and the methods within the classes, but the linked list
  274. doesn't need to know that the new class is added, so it does not
  275. require any changes in order to update the program to handle
  276. consultant class objects.  In this particular case, the linked list
  277. is very small and easy to understand, but suppose the code was very
  278. long and complex as with a large database.  It would be very
  279. difficult to update every reference to the subclasses and add
  280. another subclass to every list where they were referred to, and
  281. this operation would be very error prone.  In the present example
  282. program, the linked list would not even have to be recompiled in
  283. order to add the new functionality.
  284.  
  285. It should be clear to you that it would be possible to actually
  286. define new types, dynamically allocate them, and begin using them
  287. even while the program was executing if we properly partitioned the
  288. code into executable units operating in parallel.  This would not
  289. be easy, but it could be done for a large database that was
  290. tracking the inventory for a large retail store, or even for an
  291. airlines reservation system.  You probably have little difficulty
  292. understanding the use of dynamically allocated memory for data, but
  293.  
  294.                                                         Page 11-5
  295.  
  296.                               Chapter 11 - More Virtual Functions
  297.  
  298. dynamically allocating classes or types is new and difficult to
  299. grasp, but the possibility is there with dynamic binding.
  300.  
  301.  
  302. PROGRAMMING EXERCISES
  303. _________________________________________________________________
  304.  
  305.  
  306. 1.   Add a new class named consultant to the files named
  307.      SUPERVSR.HPP and SUPERVSR.CPP, then add code to EMPLOYE2.CPP
  308.      to exercise the new class.  Note that you do not need to
  309.      recompile the linked list class in order to execute the new
  310.      code and use the new class.  Even without recompiling the
  311.      linked list class it is capable of storing and passing the new
  312.      class of data provided of course that the new class is
  313.      referred to using a pointer to the parent class.
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.                                                         Page 11-6
  354.